Item={}
ItemFunc={}
ItemFunc.__index = ItemFunc
setmetatable(Item, ItemFunc)

ItemFunc.createWithIndex = function(self, itemIndex)
  local item = {}
  setmetatable(item, ItemFunc)
  item.index = itemIndex
  item.itemInfo = UserData.userinfo.host.items[tostring(itemIndex)]
  item.itemConfig = item:getItemConfig(item.itemInfo.i) 
  item.itemLevel = item:getItemLevel(item.itemInfo.i) 
  return item
  --item.itemConfig = UserData.config.models.Item[item.itemInfo.i] 
end

ItemFunc.createWithId = function(self, itemId)
  local item = {}
  setmetatable(item, ItemFunc)
  --item.itemInfo = UserData.userinfo.host.items[tostring(itemIndex)]
  item.itemConfig = item:getItemConfig(itemId) 
  item.itemLevel = item:getItemLevel(itemId) 
  for k,v in pairs(UserData.userinfo.host.items) do
    if v.i == itemId then
      item.itemInfo = v
    end
  end
  return item
  --item.itemConfig = UserData.config.models.Item[item.itemInfo.i] 
end
ItemFunc.createWithLevel = function(self, level)
  local item = {}
  setmetatable(item, ItemFunc)
  --item.itemInfo = UserData.userinfo.host.items[tostring(itemIndex)]
  item.itemLevel = UserData.config.models.Stone[level]
  item.itemConfig = item:getItemConfig(item.itemLevel.itemId) 
  for k,v in pairs(UserData.userinfo.host.items) do
    if v.i == item.itemLevel.itemId then
      item.itemInfo = v
    end
  end
  return item
end



ItemFunc.getItemConfig = function(self, id)
  print("ItemFunc.getItemConfig", id)
  for k,v in pairs(UserData.config.models.Item) do
    if id==v.id then
      return v
    end
  end
  return nil
end
ItemFunc.getItemLevel = function(self, id)
  for k,v in pairs(UserData.config.models.Stone) do
    if id==v.itemId then
      return v
    end
  end
  return nil
end
ItemFunc.createItemLayer = function(self, action, target, num, numOnly)
  local itemLayer = CCLayer:create()

  local imgName = nameFromPath(self.itemConfig.asset)
  print("##", k, imgName)
  local button = CCButton:createWithFileName(action, imgName, imgName, imgName, "")
  setObjectExtTable(button, target)
  if self.index~=nil then
    button:setTag(100000+self.index)
  end
  itemLayer:addChild(button)

  local lb = nil 
  if self.itemInfo~=nil and num~=nil then
    if numOnly==true then
      lb = CCLabelTTF:create(string.format("%d", num), "Helvetica", 28)
    else
      lb = CCLabelTTF:create(string.format("%d/%d", num,self.itemInfo.n), "Helvetica", 28)
    end
    if self.itemInfo.n<num then
      lb:setColor(ccc3(255,0,0))
    end
  elseif num~=nil and self.itemInfo==nil then
    if numOnly~=nil and numOnly==true then
      lb = CCLabelTTF:create(string.format("%d", num), "Helvetica", 28)
    else
      lb = CCLabelTTF:create(string.format("%d/%d", num, 0), "Helvetica", 28)
      lb:setColor(ccc3(255,0,0))
    end
  elseif self.itemInfo~=nil and num==nil then
    lb = CCLabelTTF:create(string.format("%d", self.itemInfo.n), "Helvetica", 28)
  end
  
  itemLayer:addChild(lb)
  lb:setAnchorPoint(ccp(1,0.5))
  lb:setPosition(ccp(40, -25))
  
  return itemLayer
end

ItemFunc.createTip = function(self, target)
  local tipLayer = CCBReader:load("ItemProperty.ccbi")
  local spBK = tipLayer:getChildByTag(1001)
  local sp = CCSprite:create(nameFromPath(self.itemConfig.asset))
  sp:setPosition(spBK:getPosition())
  tipLayer:addChild(sp)
  
  setObjectExtTable(tipLayer:getChildByTag(2003), target)
  setObjectExtTable(tipLayer:getChildByTag(2002), target)
  setObjectExtTable(tipLayer:getChildByTag(2001), target)
  
  TypeCast(tipLayer:getChildByTag(1010), "CCLabelTTF"):setString(self.itemConfig.name)
  TypeCast(tipLayer:getChildByTag(1011), "CCLabelTTF"):setString(string.format("%d现金", self.itemConfig.saleCash))
  TypeCast(tipLayer:getChildByTag(1012), "CCLabelTTF"):setString(autoEnter(self.itemConfig.desc, 30))
  
  return tipLayer
end
ItemFunc.createTip1 = function(self, target)
  local tipLayer = CCBReader:load("ItemProperty1.ccbi")
  local spBK = tipLayer:getChildByTag(1001)
  local sp = CCSprite:create(nameFromPath(self.itemConfig.asset))
  sp:setPosition(spBK:getPosition())
  tipLayer:addChild(sp)
  TypeCast(tipLayer:getChildByTag(1010), "CCLabelTTF"):setString(self.itemConfig.name)
  TypeCast(tipLayer:getChildByTag(1011), "CCLabelTTF"):setString(string.format("%d现金", self.itemConfig.saleCash))
  TypeCast(tipLayer:getChildByTag(1012), "CCLabelTTF"):setString(autoEnter(self.itemConfig.desc, 30))
  return tipLayer
end
